define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user_coupon_exchange/index' + location.search,
                    // add_url: 'user/user_coupon_exchange/add',
                    // edit_url: 'user/user_coupon_exchange/edit',
                    del_url: 'user/user_coupon_exchange/del',
                    multi_url: 'user/user_coupon_exchange/multi',
                    import_url: 'user/user_coupon_exchange/import',
                    table: 'user_coupon_exchange',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");
                $("input[name='shop_id']", form)
                    .addClass("selectpage")
                    //.data("source", "shop/index")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    //.data("field", "shopname")
                    .data("field", "nickname")
                    .data("params", function (obj) {
                        //obj为SelectPage对象
                        return {custom: {group_id: ['in',"2,3"]}};
                    })
                    .data("orderBy", "id desc");
                $("input[name='goods_id']", form)
                    .addClass("selectpage")
                    .data("source", "goods/goods/index")
                    .data("primaryKey", "id")
                    .data("field", "goods_name")
                    .data("orderBy", "id desc");
                $("input[name='coupon_exchange_id']", form)
                    .addClass("selectpage")
                    .data("source", "couponexchange/index")
                    .data("primaryKey", "id")
                    .data("field", "title")
                    .data("orderBy", "id desc");
                $("input[name='claim_shop_id']", form)
                    .addClass("selectpage")
                    // .data("source", "shop/index")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    // .data("field", "shopname")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), visible: false, operate: 'LIKE %...%'},
                        {field: 'user.nickname', title: __('User_id'), operate: false},
                        {field: 'shop_id', title: __('Shop_id'), visible: false, operate: 'LIKE %...%'},
                        // {field: 'shop.shopname', title: __('Shop_id'), operate: false},
                        {field: 'shop.nickname', title: __('Shop_id'), operate: false},
                        {field: 'goods_id', title: __('Goods_id'), visible: false, operate: 'LIKE %...%'},
                        {field: 'goods.goods_name', title: __('Goods_id'), operate: false},
                        {field: 'coupon_exchange_id', title: __('Coupon_exchange_id'), visible: false, operate: 'LIKE %...%'},
                        {field: 'couponexchange.title', title: __('Coupon_exchange_id'), operate: false},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_num', title: __('Goods_num')},
                        {field: 'claim_code', title: __('Claim_code'), operate: 'LIKE'},
                        // {field: 'claim_shop_id', title: __('Claim_shop_id'), visible: false, operate: 'LIKE %...%'},
                        // {field: 'claimshop.shopname', title: __('Claim_shop_id'), operate: false},
                        // {field: 'claimshop.nickname', title: __('Claim_shop_id'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'use_time', title: __('Use_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
